﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao.Intermediarios
{
    public class IPDemanda : Intermediario
    {
        public override void save<T>(T entity)
        {
            try
            {
                Demanda dem = entity as Demanda;
                dao.Demandas.InsertOnSubmit(dem);
            }
            catch (Exception ex)
            {
                throw new Exception("IPDemanda.save - " + ex.Message);
            }
        }

        public override List<T> getAll<T>()
        {
            return (from dem in dao.Demandas orderby dem.idDemanda select dem).ToList() as List<T>;
        }
        //FALTA IMPLEMENTAR TODOS LOS DEMÁS!!!!!
        public override List<T> getByCriterio<T>(string att, string op, string val)
        {
            return (from dem in dao.Demandas where dem.periodo == Int32.Parse(att) && dem.IdProducto == Int16.Parse(val) select dem).ToList() as List<T>;
        }

        public override IList<T> getCriterioById<T>(string att, string op, int val)
        {
            return (from dem in dao.Demandas where dem.IdProducto == Convert.ToInt16(val) select dem).ToList() as List<T>;
            //throw new NotImplementedException();
        }

        public override object getPorId(int id)
        {
            return (from est in dao.RF_CTs where est.IdCT == id select est);
        }

        public IList<RF_CT> getCCId(int id)
        {
            return (from est in dao.RF_CTs where est.IdCT == id select est).ToList();
        }

        public CentroTrabajo getById(int id)
        {
            try
            {
                return (from ct in dao.CentroTrabajos where ct.IdCentroTrab == id select ct).SingleOrDefault();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public override IList<T> getPorCriterio<T>(string name)
        {
            throw new NotImplementedException();
        }

        public override void Update<T>(T entity)
        {
            throw new NotImplementedException();
        }

        public override void Delete(int id)
        {
            try
            {
                var ct = (from c in dao.CentroTrabajos where c.IdCentroTrab == id select c).SingleOrDefault();

                if (ct != null)
                {
                    dao.CentroTrabajos.DeleteOnSubmit(ct);
                    dao.SubmitChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}
